package src.main.其他模式.业务代表模式;

public class BusinessDelegatePatternDemo {
    public static void main(String[] args) {

        BusinessDelegate businessDelegate = new BusinessDelegate();
        businessDelegate.setServiceType("EJB");

        Client client = new Client(businessDelegate);
        client.doTask();

        businessDelegate.setServiceType("JMS");
        client.doTask();
    }
}

interface BusinessService {
    void doProcessing();
}

class EJBService implements BusinessService {
    @Override
    public void doProcessing() {
        System.out.println("调用EJB服务");
    }
}

class JMSService implements BusinessService {
    @Override
    public void doProcessing() {
        System.out.println("调用JMS服务");
    }
}

class BusinessLookUp {
    public BusinessService getBusinessService(String serviceType){
        if(serviceType.equalsIgnoreCase("EJB")){
            return new EJBService();
        }else {
            return new JMSService();
        }
    }
}

class BusinessDelegate {
    private BusinessLookUp lookupService = new BusinessLookUp();
    private BusinessService businessService;
    private String serviceType;

    public void setServiceType(String serviceType){
        this.serviceType = serviceType;
    }

    public void doTask(){
        businessService = lookupService.getBusinessService(serviceType);
        businessService.doProcessing();
    }
}

class Client {
    BusinessDelegate businessService;
    public Client(BusinessDelegate businessService){
        this.businessService  = businessService;
    }
    public void doTask(){
        businessService.doTask();
    }
}
